package Models;

import java.util.ArrayList;

public class Desk {

	public void addUser(String netID, int roomNum, String first, String last,
			boolean isRA, String workerID) throws CredCheckError {

		if (credentialCheck(workerID)) {
			netID = netID.toLowerCase();
			User u = new User(netID, first, last, roomNum, isRA);
			DBSupport.getDBSupportInstance().putUser(u);
		} else
			throw new CredCheckError();
	}

	public void addEquipment(String name, int checkOutDays,
			String workerID) throws CredCheckError {
		if (credentialCheck(workerID)) {
			ArrayList<Equipment> arr = new ArrayList<Equipment>(DBSupport.getDBSupportInstance().getAllEquipment());
			Equipment e = new Equipment(name, arr.size()+1, checkOutDays);
			DBSupport.getDBSupportInstance().putEquipment(e);
		} else
			throw new CredCheckError();
	}

	public boolean removeEquipment(int ID, String workerID)
			throws CredCheckError {
		if (credentialCheck(workerID)) {
			ArrayList<Equipment> equip = new ArrayList<Equipment>(DBSupport
					.getDBSupportInstance().getAllEquipment());
			for (int i = 0; i < equip.size(); i++) {
				Equipment e = equip.get(i);
				if (e.getID() == ID) {
					DBSupport.getDBSupportInstance().removeEquipment(ID);
					return true;
				}
			}
			return false;
		} else
			throw new CredCheckError();
	}

	public boolean removeUser(String netID, String workerID){
		if (credentialCheck(workerID)) {
			DBSupport.getDBSupportInstance().removeUser(netID);
			return true;
		}
		return false;
	}

	public ArrayList<Equipment> search(String query) {
		ArrayList<Equipment> equip = new ArrayList<Equipment>(DBSupport.getDBSupportInstance().getAllEquipment());
		ArrayList<Equipment> matches = new ArrayList<Equipment>();
		for (int i = 0; i < equip.size(); i++) {
			Integer intID = equip.get(i).getID();
			String stringID = intID.toString();

			// check for fields that match (completely or partially) the query
			if (equip.get(i).getName().contains(query)
					|| stringID.contains(query)) {
				matches.add(equip.get(i));
			}
		}

		return matches;
	}

	public ArrayList<Equipment> overdueEquipment() {
		ArrayList<Equipment> equipment = new ArrayList<Equipment>(DBSupport
				.getDBSupportInstance().getAllEquipment());
		ArrayList<Equipment> overdue = new ArrayList<Equipment>();
		for (int i = 0; i < equipment.size(); i++) {
			if (equipment.get(i).isOverdue())
				overdue.add(equipment.get(i));
		}
		return overdue;
	}

	public Equipment selectEquipment(int ID) {
		return DBSupport.getDBSupportInstance().getEquipment(ID);
	}

	public void checkOut(String netIDStudent, String netIDWorker, int ID)
			throws CredCheckError, CheckedOutException {
		if (credentialCheck(netIDWorker)) {
			DBSupport db = DBSupport.getDBSupportInstance();
			Equipment e = db.getEquipment(ID);
			e.checkOut(netIDStudent);
			db.putEquipment(e);
		} else {
			throw new CredCheckError();
		}
	}

	public void extendCheckOut(String netIDWorker, int ID)
			throws CredCheckError {
		if (credentialCheck(netIDWorker)) {
			DBSupport db = DBSupport.getDBSupportInstance();
			Equipment e = db.getEquipment(ID);
			e.extendCheckout();
			db.putEquipment(e);
		} else
			throw new CredCheckError();
	}

	public ArrayList<Integer> userHistory(String netID) {
		DBSupport db = DBSupport.getDBSupportInstance();
		User user = db.getUser(netID);
		return user.getHistory();
	}

	private boolean credentialCheck(String workerID) {
		DBSupport db = DBSupport.getDBSupportInstance();
		User u = db.getUser(workerID);
		try{
			return u.getIsRA();
		} catch(NullPointerException e){
			return false;
		}
	}

	public void checkIn(String netIDWorker, int ID) throws CredCheckError {
		if(credentialCheck(netIDWorker)) DBSupport.getDBSupportInstance().getEquipment(ID).checkIn();
		else throw new CredCheckError();
	}
	

}
